<?php

class Damages_Methods_Data_Permission {

  /**
   *
   * @var int 
   */
  public $id;

  /**
   *
   * @var string 
   */
  public $name;

  /**
   *
   * @var string 
   */
  public $code;

  /**
   *
   * @var Damages_Methods_Data_Processes_ProcessType[] 
   */
  public $processes = array();

  /**
   *
   * @var Damages_Methods_Data_List_Field[] 
   */
  public $fields = array();

  /**
   *
   * @var Damages_Methods_Data_List_Filter[] 
   */
  public $filters = array();

  public function __construct(DamagesPermissions $permission, $useUserPermissions = true, $skipDetails = false) {
    $this->id = $permission->getId();
    $this->name = $permission->getName();
    $this->code = $permission->getCode();
    if (!$skipDetails){
      $this->setFieldsPermisions($useUserPermissions);
      $this->setFiltersPermisions($useUserPermissions);
      $this->setProcessPermissions($useUserPermissions);
    }
  }

  private function setProcessPermissions($useUserPermissions = true) {
    if ($useUserPermissions === false)
      $processTypesPermissions = DamagesProcessTypePermissionsQuery::create()
              ->useProcessTypesQuery()
              ->useProcessTypeActionsQuery()
              ->joinActions()
              ->joinProcessTypeActionsPermissions()
              ->endUse()
              ->endUse()
              ->addJoinCondition('ProcessTypeActionsPermissions', sprintf('%s = %s', DamagesProcessTypeActionsPermissionsPeer::PERMISSION_ID, DamagesProcessTypePermissionsPeer::PERMISSION_ID))
              ->filterByPermissionId($this->id)
              ->with('ProcessTypes')
              ->with('ProcessTypeActions')
              ->with('Actions')
              ->find();
    else
      $processTypesPermissions = Damages_Auth::getInstance()->getProcessTypesPermissions();
    foreach ($processTypesPermissions as $processTypePermission) {
      $this->processes[] = new Damages_Methods_Data_Processes_ProcessType($processTypePermission->getProcessTypes());
      $processTypePermission->clearAllReferences(true);
    }
  }

  private function setFiltersPermisions($useUserPermissions = true) {
    if ($useUserPermissions === false)
      $filtersPermissions = DamagesPermissionListFiltersQuery::create()
              ->joinListFilters()
              ->filterByPermissionId($this->id)
              ->with('ListFilters')
              ->find();
    else
      $filtersPermissions = Damages_Auth::getInstance()->getFiltersPermissions();
    foreach ($filtersPermissions as $permissionListFilter) {
      $this->filters[] = new Damages_Methods_Data_List_Filter($permissionListFilter->getListFilters());
    }
  }

  private function setFieldsPermisions($useUserPermissions = true) {
    if ($useUserPermissions === false)
      $fieldsPermissions = DamagesPermissionListFieldsQuery::create()
              ->joinListFields()
              ->filterByPermissionId($this->id)
              ->with('ListFields')
              ->find();
    else
      $fieldsPermissions = Damages_Auth::getInstance()->getFieldsPermissions();
    foreach ($fieldsPermissions as $permissionListField) {
      $this->fields[] = new Damages_Methods_Data_List_Field($permissionListField->getListFields());
    }
  }

}